package com.mairuide.one_click_search.controller;

import com.mairuide._frame.base.BaseController;
import com.mairuide._frame.utils.ReflectUtils;
import com.mairuide.bill.utils.DownloadUtils;
import com.mairuide.entity.PageResult;
import com.mairuide.entity.Result;
import com.mairuide.entity.StatusCode;
import com.mairuide.one_click_search.dto.DataTemplateDto;
import com.mairuide.one_click_search.pojo.DataSource;
import com.mairuide.one_click_search.pojo.DataTemplateField;
import com.mairuide.one_click_search.service.DataSourceService;
import com.mairuide.one_click_search.service.DataTemplateFieldService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @auther 刘中华
 * @create 2019-04-24 9:31
 */
@Controller
@RequestMapping("onekey_dataSource")
public class DataSourceController extends BaseController {

    @Autowired
    private DataSourceService dataSourceService;

    @Autowired
    private DataTemplateFieldService dataTemplateFieldService;
    /**
     * @Author 刘中华
     * @Date 2019/5/7 11:18
     * @Param [tablename]
     * @return java.lang.Object
     * @Description  返回模板数据列表页面
     **/
    @RequestMapping(method= RequestMethod.GET,value = "list/{tablename}")
    public Object page(@PathVariable(name = "tablename") String tablename){
        request.setAttribute("tablename",tablename);
        return "/one_click_search/dataList";
    }

    @RequestMapping("/list/{page}/{size}")
    @ResponseBody
    public Result DataSourceList(@RequestBody DataTemplateDto dataTemplateDto, @PathVariable int page, @PathVariable int size){
        PageResult pageResult = dataSourceService.getListByTableName(dataTemplateDto,page,size);
        return new Result(true, StatusCode.OK,"操作成功",pageResult);
    }

    @RequestMapping("/export")
    @ResponseBody
    public void DataSourceExport(String ids,String tableName,Integer type) throws Exception{
        List<DataSource> dataSourceByIds = dataSourceService.getDataSourceByIds(ids,tableName,type);
        //2.构造Excel
        SXSSFWorkbook wb = new SXSSFWorkbook(100); //阈值，内存中的对象数量最大数量
        //构造sheet
        Sheet sheet = wb.createSheet();
        //标题
        Map<String, DataTemplateField> colToFieldMap = dataTemplateFieldService.getColToFieldMap(tableName);
        String s="";
        for (Map.Entry<String, DataTemplateField> entry : colToFieldMap.entrySet()) {
            s+=entry.getValue().getFiledname()+",";
        }
        String [] titles = s.split(",");
        Row row = sheet.createRow(0);
        int titleIndex=0;
        for (String title : titles) {
            Cell cell = row.createCell(titleIndex++);
            cell.setCellValue(title);
        }
        int rowIndex = 1;
        Cell cell=null;
        for (DataSource employeeReportResult : dataSourceByIds) {
            row = sheet.createRow(rowIndex++);
            int colIndex = 0;
            for (Map.Entry<String, DataTemplateField> entry : colToFieldMap.entrySet()) {
                cell = row.createCell(colIndex++);
                try {
                    Object getMethod = ReflectUtils.getGetMethod(employeeReportResult, entry.getValue().getColname());
                    if (getMethod!=null){
                        cell.setCellValue(getMethod.toString());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        //3.完成下载
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        wb.write(os);
        new DownloadUtils().download(os,response,"一键搜导出数据.xlsx");
    }
}
